var f = "";
var listParam = "";
var saveParam = "";
$(function () {
    $("#grid").datagrid({
        url: name + '_list' + listParam,
        columns: columns,
        singleSelect: true,
        autoRowHeight:true,
        striped:true,
        pagination: true,
       // fitColumns: true,
        loadMsg:"数据加载中....",
        toolbar: [{
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                f = "add";
                $('#editDlg').dialog('open');
            }
        }, '-', {
            text: '导出',
            iconCls: 'icon-excel',
            handler: function () {
                var formData = $('#searchForm').serializeJSON();
                $.download(name + "_export" + listParam, formData);
            }
        }, '-', {
            text: '导入',
            iconCls: 'icon-save',
            handler: function () {
                $('#importDlg').dialog('open');
            }
        }]
    });

    var h = 200;
    var w = 300;
    var title = ""
    var myButtons = []
    if (typeof (height) != "undefined") {
        h = height;
    }

    if (typeof (width) != "undefined") {
        w = width;
    }

    if (typeof (title_) != "undefined") {
        title = title_;
    }

    if (typeof (my_Buttons) != "undefined") {
        myButtons = my_Buttons;
    }

    $('#editDlg').dialog({
        title: title,
        width: w,
        height: h,
        closed: true,//窗口是是否为关闭状态, true：表示关闭
        modal: true,//模式窗口
        cache: false,
        buttons:myButtons,
        onclose:function () {
           reloadGrid();
        }
    });

    $('#btnSave').bind('click', function () {
        var isValid = $("#editForm").form('validate');
        if (isValid == false) return;
        var formData = $('#editForm').serializeJSON();
        $.ajax({
            url: name + '_' + f + saveParam,
            data: formData,
            dataType: 'json',
            type: 'post',
            success: function (rtn) {
                $.messager.alert("提示", rtn.message, 'info', function () {
                    //成功的话，我们要关闭窗口
                    $('#editDlg').dialog('close');
                    $('#editForm').form('clear');
                    //刷新表格数据
                    $('#grid').datagrid('reload');
                });
            }
        });
    });

    $('#btnSearch').bind('click', function () {
        //把表单数据转换成json对象
        var formData = $('#searchForm').serializeJSON();
        $('#grid').datagrid('load', formData);
    });

    //判断是否有导入数据的功能
    var importForm = $('#importForm');
    if (importForm) {
        $('#importDlg').dialog({
            title: '导入数据',
            width: 330,
            height: 106,
            modal: true,
            closed: true,
            buttons: [
                {
                    text: '导入',
                    handler: function () {
                        $.ajax({
                            url: name + '_doImport',
                            data: new FormData($('#importForm')[0]),
                            type: 'post',
                            processData: false,
                            contentType: false,
                            dataType: 'json',
                            success: function (res) {
                                $.messager.alert('提示', res.message, 'info', function () {
                                    if (res.success) {
                                        $('#importDlg').dialog('close');
                                        $('#importForm').form('clear');
                                        $('#grid').datagrid('reload');
                                    }
                                });
                            }
                        });
                    }
                }
            ]
        });
    }

})

function edit(uuid) {
    console.log(uuid);
    //弹出窗口
    $('#editDlg').dialog('open');

    //清空表单内容
    $('#editForm').form('clear');
    f = "update";
    //加载数据
    $('#editForm').form('load', name + '_get?id=' + uuid);
}

function del(uuid) {
    $.messager.confirm("确认", "确认要删除吗？", function (yes) {
        if (yes) {
            $.ajax({
                url: name + '_delete?id=' + uuid,
                dataType: 'json',
                type: 'post',
                success: function (rtn) {
                    $.messager.alert("提示", rtn.message, 'info', function () {
                        //刷新表格数据
                        exceuct();
                    });
                }
            });
        }
    });
}

function reloadGrid() {
    $('#grid').datagrid('reload');
}

function fh(uuid) {
    $.messager.confirm("确认", "确认要发货吗？", function (yes) {
        if (yes) {
            $.ajax({
                url: name + '_fh?id=' + uuid,
                dataType: 'json',
                type: 'post',
                success: function (rtn) {
                    $.messager.alert("提示", rtn.message, 'info', function () {
                        //刷新表格数据
                        $('#grid').datagrid('reload');
                    });
                }
            });
        }
    });
}